<?php
/*
*用於後臺的菜單 By yan
*對應的顯示i18n是 menu
*/
namespace yan\backendclient\widgets;

use Yii;
use yii\helpers\Html;
use yii\helpers\Url;

use yan\backendclient\models\Menu;

class BackendMenu extends \yii\base\Widget
{
	public $controller_id;
	public $module_id;
	public $action_id;
	public $type_id;

	public function init(){
		$this->controller_id = Yii::$app->controller->id;
		
		$this->module_id = Yii::$app->controller->module->id;
		$this->module_id = $this->module_id == APP_NAME . '-backend' ? null : $this->module_id;
		
		$this->action_id = Yii::$app->controller->action->id;
		
		$this->type_id = Yii::$app->request->get('type');
	}
    public function run(){
    	// 第一級目錄
    	$firstLi = $this->findArray();
    	$shtml = '';

    	foreach ((array)$firstLi as $firstKey => $first) {

    		// 二級數據處理
    		$secondLi = $this->findArray($first['id']);

    		$secondShtml = '';
    		$second_is_active = false;
            $flag_first_show = false;
    		foreach ((array)$secondLi as $secondkey => $second) {
    			$second_url = Menu::setBackendUrl($second);
    			$second_active = $this->isActive($second);

                $flag_show = self::checkShow($second_url);

                $flag_first_show = $flag_show ? true : $flag_first_show;

    			$second_is_active = $second_active ? true : $second_is_active;

    			$menu_name = !empty($second['i18n']) ? Yii::t('menu', $second['i18n']) : $second['name'];
    			$menu_name = Html::a($menu_name, $second_url);
                if( $flag_show )
    			     $secondShtml .= Html::tag('li', $menu_name, ['class'=>$second_active ? 'current-page' : '']);
    		}

            

    		$secondShtml = empty($secondLi) ? '' : Html::tag('ul', $secondShtml, ['class'=>'nav child_menu', 'style'=>($second_is_active ? 'display:block' : '')]);
    		// 一級處理
    		$first_url = !empty($secondLi) ? 'javascript:;' : Menu::setBackendUrl($first);
    		$first_active = $second_is_active ? true : ( !empty($secondLi) ? false : $this->isActive($first) );

            if( self::checkShow($first_url) ){
                $flag_first_show = true;
            }

    		$first_name = !empty($first['i18n']) ? Yii::t('menu', $first['i18n']) : $first['name'];
    		$first_i = !empty($first['class']) ? Html::tag('i', '', ['class'=>$first['class']]) : '';
    		
    		$first_name = Html::a($first_i . $first_name . (!empty($secondLi) ? '<span class="fa fa-chevron-down"></span>' : ''),  $first_url);

    		$firstHtml = Html::tag('li', $first_name . $secondShtml, ['class'=>($first_active ? 'active' : '')]);
    		if( $flag_first_show )
    		  $shtml .= $firstHtml;
    	}
    	return Html::tag('ul', $shtml, ['class'=>'nav side-menu']);
    }
    // 判斷是否為active 
    public function isActive($data){
    	// 如果url 不為空的時候
    	if( !empty($data['url']) && preg_match("/^\//", $data['url']) ){
    		$url = Url::to([$data['url']]);

    		return strpos(Yii::$app->request->getUrl(), $url) !== false;
    	}else if( !empty($data['url']) ){
    		// 連接不是r的路徑規則時
    		return strpos(Yii::$app->request->getUrl(), $data['url']) !== false;
    	}else {
    		if( $this->controller_id == $data['controller_id'] && 
    			$this->module_id == $data['module_id'] &&
    			$this->type_id == $data['type']
    			){
    			if( $this->action_id != 'index' && $data['action_id']=='index' ){
    				return true;
    			}
    			return $this->action_id == $data['action_id'];
    		}
    	}
    	return false;
    }
    // 獲取菜單數據
    protected function findArray($pid = 0){
    	$data = Menu::find()->where(['pid'=>$pid])
                ->andWhere(['status'=>1])
    			->orderBy('sort_order asc')
    			->asArray()->all();
    	
    	return $data;
    }

    public static function urlToRole($url){
        $route = is_array($url) ? $url['0'] : $url;

        $route = explode('/', $route);

        if( empty($route['0']) ){
            unset($route['0']);
        }
        $str_route = implode('.', $route);
        return $str_route;
    }

    public static function checkShow($url){
        if( Yii::$app->user->identity->power == 'admin' ) return true;
        $route = self::urlToRole($url);
        $flagRoute = Yii::$app->acl->isAllow($route);
        return $flagRoute;
    }
}
